package com.greatwqs.wmall.order.provider.util;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 同一毫秒内递增序列号
 *
 * @author wang-qingsong
 */
public class SequenceUtils {

    private static volatile long lastTimestamp = 0L;
    private static final AtomicInteger sequence = new AtomicInteger(0);

    public static synchronized long getSequenceId(long currentMs) {
        if (currentMs > lastTimestamp) {
            // 时间进入新的一毫秒，重置序列号
            sequence.set(0);
            lastTimestamp = currentMs;
        }

        // 同一毫秒内递增序列号
        return sequence.incrementAndGet();
    }
}